<!DOCTYPE html>












  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">









  
  
    
  
  <link rel="stylesheet" media="all" href="https://cdnjs.cloudflare.com/ajax/libs/Han/3.3.0/han.min.css">




<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">



















  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=6.3.0" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.3.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.3.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '6.3.0',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="请求代理模式proxy_pass中只有IP和端口, 称为不带URI, 如:proxy_pass http://1270.0.0.1:8080, 另外一种是端口号之后包含其他路径的, 如: proxy_pass http://127.0.0.1:8080/(哪怕只带了一个/) 或 proxy_pass http://127.0.0.1:8080/test  对于不带URI的方式, nginx会保留l">
<meta name="keywords" content="linux,nginx">
<meta property="og:type" content="article">
<meta property="og:title" content="nginx config">
<meta property="og:url" content="http://codertang.com/2020/06/22/nginx-config/index.html">
<meta property="og:site_name" content="Tonny&#39;s Blog">
<meta property="og:description" content="请求代理模式proxy_pass中只有IP和端口, 称为不带URI, 如:proxy_pass http://1270.0.0.1:8080, 另外一种是端口号之后包含其他路径的, 如: proxy_pass http://127.0.0.1:8080/(哪怕只带了一个/) 或 proxy_pass http://127.0.0.1:8080/test  对于不带URI的方式, nginx会保留l">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2021-12-09T09:07:35.260Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="nginx config">
<meta name="twitter:description" content="请求代理模式proxy_pass中只有IP和端口, 称为不带URI, 如:proxy_pass http://1270.0.0.1:8080, 另外一种是端口号之后包含其他路径的, 如: proxy_pass http://127.0.0.1:8080/(哪怕只带了一个/) 或 proxy_pass http://127.0.0.1:8080/test  对于不带URI的方式, nginx会保留l">






  <link rel="canonical" href="http://codertang.com/2020/06/22/nginx-config/">



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>nginx config | Tonny's Blog</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Tonny's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">莫等闲, 白了少年头, 空悲切</p>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>
  </li>

      
      
    </ul>
  

  
    

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://codertang.com/2020/06/22/nginx-config/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tonny Yi">
      <meta itemprop="description" content="Coding, Life, Zen">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tonny's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">nginx config
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2020-06-22 18:44:29" itemprop="dateCreated datePublished" datetime="2020-06-22T18:44:29+08:00">2020-06-22</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2021-12-09 17:07:35" itemprop="dateModified" datetime="2021-12-09T17:07:35+08:00">2021-12-09</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/linux/nginx/" itemprop="url" rel="index"><span itemprop="name">nginx</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        <h2 id="请求代理模式"><a href="#请求代理模式" class="headerlink" title="请求代理模式"></a>请求代理模式</h2><p><code>proxy_pass</code>中只有IP和端口, 称为不带URI, 如:<code>proxy_pass http://1270.0.0.1:8080</code>, 另外一种是端口号之后包含其他路径的, 如: <code>proxy_pass http://127.0.0.1:8080/</code>(哪怕只带了一个<code>/</code>) 或 <code>proxy_pass http://127.0.0.1:8080/test</code></p>
<ul>
<li><p><strong>对于不带URI的方式, nginx会保留location中的路径部分</strong>, 如: </p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">location</span> /api1/ &#123;</span><br><span class="line">    <span class="attribute">proxy_pass</span> http://127.0.0.1:8080;</span><br><span class="line">&#125;</span><br><span class="line">http://127.0.0.1/api1/xxx  -&gt;  http://127.0.0.1:8080/api1/xxx</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>对于带URI的方式, nginx会对URL进行替换</strong></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">location</span> /api2/ &#123;</span><br><span class="line">    <span class="attribute">proxy_pass</span> http://127.0.0.1:8080/;</span><br><span class="line">&#125;</span><br><span class="line">http://127.0.0.1/api2/xxx  -&gt;  http://127.0.0.1:8080/xxx</span><br></pre></td></tr></table></figure>
<p><code>http://127.0.0.1/api2/</code>被替换成<code>http://127.0.0.1:8080/</code>, 再加上剩下的<code>xxx</code>, 最终成为<code>http://127.0.0.1:8080/xxx</code></p>
<p>又比如:</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">location</span> /api3/ &#123;</span><br><span class="line">    <span class="attribute">proxy_pass</span> http://127.0.0.1:8080/test;</span><br><span class="line">&#125;</span><br><span class="line">http://127.0.0.1/api3/xxx  -&gt; http://127.0.0.1:8080/testxxx</span><br></pre></td></tr></table></figure>
<p><code>http://127.0.0.1/api3/</code>被替换成了<code>http://127.0.0.1:8080/test</code>, 再加上剩余的<code>xxx</code>, 就变成了<code>http://127.0.0.1:8080/testxxx</code></p>
</li>
</ul>
<h3 id="规则总结"><a href="#规则总结" class="headerlink" title="规则总结"></a>规则总结</h3><p>nginx代理8080端口, 转发到8989端口, 客户端请求地址: <a href="http://localhost:8080/api/user" target="_blank" rel="noopener">http://localhost:8080/api/user</a>, nginx配置格式如下:</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">server</span> &#123;</span><br><span class="line">    <span class="attribute">listen</span>       <span class="number">8080</span>;</span><br><span class="line">    <span class="attribute">access_log</span>  logs/host.access.log  main;</span><br><span class="line"></span><br><span class="line">	<span class="attribute">location</span> /api &#123;</span><br><span class="line">    	<span class="attribute">proxy_pass</span> http://localhost:8989/service/;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>location</code>和<code>proxy_pass</code>不同配置搭配, 转发结果如下:</p>
<table>
<thead>
<tr>
<th></th>
<th>location /api</th>
<th>location /api/</th>
</tr>
</thead>
<tbody>
<tr>
<td>proxy_pass <a href="http://localhost:8989" target="_blank" rel="noopener">http://localhost:8989</a>;</td>
<td><a href="http://localhost:8989/api/user" target="_blank" rel="noopener">http://localhost:8989/api/user</a></td>
<td><a href="http://localhost:9809/api/user" target="_blank" rel="noopener">http://localhost:9809/api/user</a></td>
</tr>
<tr>
<td>proxy_pass <a href="http://localhost:8989/" target="_blank" rel="noopener">http://localhost:8989/</a>;</td>
<td><a href="http://localhost:8989//user" target="_blank" rel="noopener">http://localhost:8989//user</a></td>
<td><strong><a href="http://localhost:8989/user" target="_blank" rel="noopener">http://localhost:8989/user</a></strong></td>
</tr>
<tr>
<td>proxy_pass <a href="http://localhost:8989/service" target="_blank" rel="noopener">http://localhost:8989/service</a>;</td>
<td><strong><a href="http://localhost:8989/service/user" target="_blank" rel="noopener">http://localhost:8989/service/user</a></strong></td>
<td><a href="http://localhost:8989/serviceuser" target="_blank" rel="noopener">http://localhost:8989/serviceuser</a></td>
</tr>
<tr>
<td>proxy_pass <a href="http://localhost:8989/service/" target="_blank" rel="noopener">http://localhost:8989/service/</a>;</td>
<td><a href="http://localhost:8989/service//user" target="_blank" rel="noopener">http://localhost:8989/service//user</a></td>
<td><strong><a href="http://localhost:8989/service/user" target="_blank" rel="noopener">http://localhost:8989/service/user</a></strong></td>
</tr>
</tbody>
</table>
<blockquote>
<p>建议: <strong>location 和 proxy_pass结尾斜线保持一致</strong></p>
</blockquote>
<h2 id="本地资源模式"><a href="#本地资源模式" class="headerlink" title="本地资源模式"></a>本地资源模式</h2><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">location</span> / &#123;</span><br><span class="line">    <span class="attribute">root</span>   /tmp/html;	<span class="comment"># root /tmp/html/; 结果一样</span></span><br><span class="line">&#125;</span><br><span class="line">localhost:8080  -&gt;  /tmp/html/index.html</span><br><span class="line">localhost:8080/index.html  -&gt;  /tmp/html/index.html</span><br><span class="line">localhost:8989/api  -&gt; 301重定向http://localhost:8989/api/</span><br><span class="line">localhost:8989/api/  -&gt;  /tmp/html/api/index.html</span><br><span class="line">localhost:8989/api/index.html  -&gt;  /tmp/html/api/index.html</span><br></pre></td></tr></table></figure>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># location 和 root 结尾有没有斜线都一样</span></span><br><span class="line"><span class="attribute">location</span> /img &#123;</span><br><span class="line">    <span class="attribute">root</span>   /tmp/html;</span><br><span class="line">&#125;</span><br><span class="line">localhost:8989/img/a.jpg  -&gt;  /tmp/html/img/a.jpg</span><br><span class="line">localhost:8989/img/api/a.jpg  -&gt; /tmp/html/img/api/a.jpg</span><br></pre></td></tr></table></figure>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">location</span> /img &#123;</span><br><span class="line">    <span class="attribute">alias</span>	/tmp/html/;</span><br><span class="line">&#125;</span><br><span class="line">localhost:8989/img/a.jpg  -&gt;  /tmp/html/a.jpg</span><br><span class="line">localhost:8989/img/api/a.jpg  -&gt; /tmp/html/api/a.jpg</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li><p>root配置时, 访问的资源是root+location目录下的</p>
</li>
<li><p>alias配置时, 访问的资源是alias目录下. <strong>如果Path不加斜线则不影响, 如果Path以斜线结尾, alias必须以斜线结尾</strong></p>
</li>
</ul>
</blockquote>
<h2 id="nginx路由匹配规则"><a href="#nginx路由匹配规则" class="headerlink" title="nginx路由匹配规则"></a>nginx路由匹配规则</h2><p>常见的路由匹配符号有:</p>
<ol>
<li><code>＝</code>：精确匹配</li>
<li><code>^~</code>：精确前缀匹配</li>
<li><code>~</code>：区分大小写的正则匹配;  <code>~*</code>：不区分大小写的正则匹配</li>
<li><code>/uri</code>：普通前缀匹配</li>
<li><code>/</code>：通用匹配</li>
</ol>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">location</span> = / &#123;</span><br><span class="line">   <span class="attribute">echo</span> <span class="string">"规则A"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="attribute">location</span> = /login &#123;</span><br><span class="line">   <span class="attribute">echo</span> <span class="string">"规则B"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="attribute">location</span><span class="regexp"> ^~</span> /static/ &#123;</span><br><span class="line">   <span class="attribute">echo</span> <span class="string">"规则C"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="attribute">location</span><span class="regexp"> ^~</span> /static/files &#123;</span><br><span class="line">    <span class="attribute">echo</span> <span class="string">"规则X"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="attribute">location</span> <span class="regexp">~ \.(gif|jpg|png|js|css)$</span> &#123;</span><br><span class="line">   <span class="attribute">echo</span> <span class="string">"规则D"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="attribute">location</span> <span class="regexp">~* \.png$</span> &#123;</span><br><span class="line">   <span class="attribute">echo</span> <span class="string">"规则E"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="attribute">location</span> /img &#123;</span><br><span class="line">    <span class="attribute">echo</span> <span class="string">"规则Y"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="attribute">location</span> / &#123;</span><br><span class="line">   <span class="attribute">echo</span> <span class="string">"规则F"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<table>
<thead>
<tr>
<th>请求URI</th>
<th>匹配路由规则</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://localhost/" target="_blank" rel="noopener">http://localhost/</a></td>
<td>规则A</td>
</tr>
<tr>
<td><a href="http://localhost/login" target="_blank" rel="noopener">http://localhost/login</a></td>
<td>规则B</td>
</tr>
<tr>
<td><a href="http://localhost/register" target="_blank" rel="noopener">http://localhost/register</a></td>
<td>规则F</td>
</tr>
<tr>
<td><a href="http://localhost/static/a.html" target="_blank" rel="noopener">http://localhost/static/a.html</a></td>
<td>规则C</td>
</tr>
<tr>
<td><a href="http://localhost/static/files/a.txt" target="_blank" rel="noopener">http://localhost/static/files/a.txt</a></td>
<td>规则X</td>
</tr>
<tr>
<td><a href="http://localhost/a.png" target="_blank" rel="noopener">http://localhost/a.png</a></td>
<td>规则D</td>
</tr>
<tr>
<td><a href="http://localhost/a.PNG" target="_blank" rel="noopener">http://localhost/a.PNG</a></td>
<td>规则E</td>
</tr>
<tr>
<td><a href="http://localhost/img/a.gif" target="_blank" rel="noopener">http://localhost/img/a.gif</a></td>
<td>规则D</td>
</tr>
<tr>
<td><a href="http://localhost/img/a.tiff" target="_blank" rel="noopener">http://localhost/img/a.tiff</a></td>
<td>规则Y</td>
</tr>
</tbody>
</table>
<blockquote>
<p>同优先级精确度越高, 优先级越高</p>
<p>同级别的定义顺序越靠前, 优先级越高</p>
</blockquote>
<h2 id="负载均衡配置"><a href="#负载均衡配置" class="headerlink" title="负载均衡配置"></a>负载均衡配置</h2><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">upstream</span> groups1 &#123;</span><br><span class="line">    <span class="comment">#  配置了不同权重</span></span><br><span class="line">    <span class="attribute">server</span> <span class="number">192.168.1.110:80</span> weight=<span class="number">10</span>;</span><br><span class="line">    <span class="attribute">server</span> <span class="number">192.168.1.111:81</span> weight=<span class="number">3</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="attribute">location</span> /a/ &#123;</span><br><span class="line">    <span class="attribute">proxy_pass</span> http://groups1/;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="典型配置"><a href="#典型配置" class="headerlink" title="典型配置"></a>典型配置</h3><p>可以通过<a href="https://www.digitalocean.com/community/tools/nginx" target="_blank" rel="noopener">digitalocean</a>的页面配置需要的功能, 然后下载生成好的配置文件</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">user</span>                 www-data;</span><br><span class="line"><span class="attribute">pid</span>                  /run/nginx.pid;</span><br><span class="line"><span class="attribute">worker_processes</span>     auto;</span><br><span class="line"><span class="attribute">worker_rlimit_nofile</span> <span class="number">65535</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># Load modules</span></span><br><span class="line"><span class="attribute">include</span>              /etc/nginx/modules-enabled/<span class="regexp">*.conf</span>;</span><br><span class="line"></span><br><span class="line"><span class="section">events</span> &#123;</span><br><span class="line">    <span class="attribute">multi_accept</span>       <span class="literal">on</span>;</span><br><span class="line">    <span class="attribute">worker_connections</span> <span class="number">65535</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="section">http</span> &#123;</span><br><span class="line">	<span class="comment"># load balence</span></span><br><span class="line">	<span class="attribute">upstream</span> user &#123;</span><br><span class="line">		<span class="attribute">server</span> <span class="number">192.168.1.100:8080</span> weight=<span class="number">1</span> max_fails=<span class="number">2</span> fail_timeout=<span class="number">30s</span>;</span><br><span class="line">		<span class="attribute">server</span> <span class="number">192.168.1.101:8080</span> weight=<span class="number">1</span> max_fails=<span class="number">2</span> fail_timeout=<span class="number">30s</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment"># nginx TO server 最大空闲连接数, 长连接数量的10%-30%</span></span><br><span class="line">		<span class="attribute">keepalive</span>	<span class="number">300</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment"># client TO nginx</span></span><br><span class="line">    <span class="attribute">keepalive_timeout</span>  <span class="number">120s</span> <span class="number">120s</span>;</span><br><span class="line">    <span class="attribute">keepalive_requests</span> <span class="number">10000</span>;</span><br><span class="line"></span><br><span class="line">    <span class="attribute">charset</span>                utf-<span class="number">8</span>;</span><br><span class="line">    <span class="attribute">sendfile</span>               <span class="literal">on</span>;</span><br><span class="line">    <span class="attribute">tcp_nopush</span>             <span class="literal">on</span>;</span><br><span class="line">    <span class="attribute">tcp_nodelay</span>            <span class="literal">on</span>;</span><br><span class="line">    <span class="attribute">server_tokens</span>          <span class="literal">off</span>;</span><br><span class="line">    <span class="attribute">types_hash_max_size</span>    <span class="number">2048</span>;</span><br><span class="line">    <span class="attribute">types_hash_bucket_size</span> <span class="number">64</span>;</span><br><span class="line">    <span class="attribute">client_max_body_size</span>   <span class="number">16M</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># MIME</span></span><br><span class="line">    <span class="attribute">include</span>                mime.types;</span><br><span class="line">    <span class="attribute">default_type</span>           application/octet-stream;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># Logging</span></span><br><span class="line">    <span class="attribute">access_log</span>             /var/log/nginx/access.log;</span><br><span class="line">    <span class="attribute">error_log</span>              /var/log/nginx/error.log <span class="literal">warn</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># Connection header for WebSocket reverse proxy</span></span><br><span class="line">    <span class="attribute">map</span> <span class="variable">$http_upgrade</span> <span class="variable">$connection_upgrade</span> &#123;</span><br><span class="line">        <span class="attribute">default</span> upgrade;</span><br><span class="line">        ""      close;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="attribute">map</span> <span class="variable">$remote_addr</span> <span class="variable">$proxy_forwarded_elem</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># IPv4 addresses can be sent as-is</span></span><br><span class="line">        ~^[0-9.]+$        "for=$remote_addr";</span><br><span class="line"></span><br><span class="line">        <span class="comment"># IPv6 addresses need to be bracketed and quoted</span></span><br><span class="line">        ~^[0-9A-Fa-f:.]+$ "for=\"[$remote_addr]\"";</span><br><span class="line"></span><br><span class="line">        <span class="comment"># Unix domain socket names cannot be represented in RFC 7239 syntax</span></span><br><span class="line">        <span class="attribute">default</span>           <span class="string">"for=unknown"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># Load configs</span></span><br><span class="line">    <span class="attribute">include</span> /etc/nginx/conf.d/<span class="regexp">*.conf</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># localhost</span></span><br><span class="line">    <span class="section">server</span> &#123;</span><br><span class="line">        <span class="attribute">listen</span>                             <span class="number">80</span>;</span><br><span class="line">        <span class="attribute">listen</span>                             [::]:<span class="number">80</span>;</span><br><span class="line">        <span class="attribute">server_name</span>                        localhost;</span><br><span class="line">        <span class="attribute">root</span>                               /var/www/localhost/public;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># . files</span></span><br><span class="line">        <span class="attribute">location</span> <span class="regexp">~ /\.(?!well-known)</span> &#123;</span><br><span class="line">            <span class="attribute">deny</span> all;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># logging</span></span><br><span class="line">        <span class="attribute">access_log</span> /var/log/nginx/localhost.access.log;</span><br><span class="line">        <span class="attribute">error_log</span>  /var/log/nginx/localhost.<span class="literal">error</span>.log <span class="literal">warn</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># index.html fallback</span></span><br><span class="line">        <span class="attribute">location</span> / &#123;</span><br><span class="line">            <span class="attribute">try_files</span> <span class="variable">$uri</span> <span class="variable">$uri</span>/ /index.html;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># favicon.ico</span></span><br><span class="line">        <span class="attribute">location</span> = /favicon.ico &#123;</span><br><span class="line">            <span class="attribute">log_not_found</span> <span class="literal">off</span>;</span><br><span class="line">            <span class="attribute">access_log</span>    <span class="literal">off</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># robots.txt</span></span><br><span class="line">        <span class="attribute">location</span> = /robots.txt &#123;</span><br><span class="line">            <span class="attribute">log_not_found</span> <span class="literal">off</span>;</span><br><span class="line">            <span class="attribute">access_log</span>    <span class="literal">off</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># assets, media</span></span><br><span class="line">        <span class="attribute">location</span> <span class="regexp">~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$</span> &#123;</span><br><span class="line">            <span class="attribute">expires</span>    <span class="number">7d</span>;</span><br><span class="line">            <span class="attribute">access_log</span> <span class="literal">off</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># svg, fonts</span></span><br><span class="line">        <span class="attribute">location</span> <span class="regexp">~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$</span> &#123;</span><br><span class="line">            <span class="attribute">add_header</span> Access-Control-Allow-Origin <span class="string">"*"</span>;</span><br><span class="line">            <span class="attribute">expires</span>    <span class="number">7d</span>;</span><br><span class="line">            <span class="attribute">access_log</span> <span class="literal">off</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># gzip</span></span><br><span class="line">        <span class="attribute">gzip</span>            <span class="literal">on</span>;</span><br><span class="line">        <span class="attribute">gzip_vary</span>       <span class="literal">on</span>;</span><br><span class="line">        <span class="attribute">gzip_proxied</span>    any;</span><br><span class="line">        <span class="attribute">gzip_comp_level</span> <span class="number">6</span>;</span><br><span class="line">        <span class="attribute">gzip_types</span>      text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># cross-origin</span></span><br><span class="line">        <span class="attribute">add_header</span> Access-Control-Allow-Origin <span class="variable">$http_origin</span>;</span><br><span class="line">        <span class="attribute">add_header</span> Access-Control-Allow-Methods *;</span><br><span class="line">		<span class="comment"># 如果不需要跨域传递cookie, 可以删除下面两行, 并把上面的$http_origin 改为*</span></span><br><span class="line">        <span class="attribute">add_header</span> Access-Control-Allow-Credentials <span class="literal">true</span>;       </span><br><span class="line">        <span class="attribute">add_header</span> Access-Control-Max-Age <span class="number">3600</span>;</span><br><span class="line">        <span class="attribute">add_header</span> Access-Control-Allow-Headers <span class="string">'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'</span>;</span><br><span class="line">        <span class="attribute">if</span> (<span class="variable">$request_method</span> = OPTIONS)&#123;</span><br><span class="line">            <span class="attribute">return</span> <span class="number">204</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># reverse proxy</span></span><br><span class="line">        <span class="attribute">location</span> /test &#123;</span><br><span class="line">            <span class="attribute">proxy_pass</span>                         http://127.0.0.1:3000;</span><br><span class="line">            <span class="attribute">proxy_http_version</span>                 <span class="number">1</span>.<span class="number">1</span>;</span><br><span class="line">            <span class="attribute">proxy_cache_bypass</span>                 <span class="variable">$http_upgrade</span>;</span><br><span class="line"></span><br><span class="line">            <span class="comment"># Proxy headers</span></span><br><span class="line">            <span class="attribute">proxy_set_header</span> Upgrade           <span class="variable">$http_upgrade</span>;</span><br><span class="line">            <span class="attribute">proxy_set_header</span> Connection        <span class="variable">$connection_upgrade</span>;</span><br><span class="line">            <span class="attribute">proxy_set_header</span> Host              <span class="variable">$host</span>;</span><br><span class="line">            <span class="attribute">proxy_set_header</span> X-Real-IP         <span class="variable">$remote_addr</span>;</span><br><span class="line">            <span class="attribute">proxy_set_header</span> Forwarded         <span class="variable">$proxy_add_forwarded</span>;</span><br><span class="line">            <span class="attribute">proxy_set_header</span> X-Forwarded-For   <span class="variable">$proxy_add_x_forwarded_for</span>;</span><br><span class="line">            <span class="attribute">proxy_set_header</span> X-Forwarded-Proto <span class="variable">$scheme</span>;</span><br><span class="line">            <span class="attribute">proxy_set_header</span> X-Forwarded-Host  <span class="variable">$host</span>;</span><br><span class="line">            <span class="attribute">proxy_set_header</span> X-Forwarded-Port  <span class="variable">$server_port</span>;</span><br><span class="line"></span><br><span class="line">            <span class="comment"># Proxy timeouts</span></span><br><span class="line">            <span class="attribute">proxy_connect_timeout</span>              <span class="number">60s</span>;</span><br><span class="line">            <span class="attribute">proxy_send_timeout</span>                 <span class="number">60s</span>;</span><br><span class="line">            <span class="attribute">proxy_read_timeout</span>                 <span class="number">60s</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>

    

    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Tonny Yi</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://codertang.com/2020/06/22/nginx-config/" title="nginx config">http://codertang.com/2020/06/22/nginx-config/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc/4.0/" rel="external nofollow" target="_blank">CC BY-NC 4.0</a> 许可协议。转载请注明出处！</li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/linux/" rel="tag"># linux</a>
          
            <a href="/tags/nginx/" rel="tag"># nginx</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/05/08/logger-level-modifier/" rel="next" title="动态调整日志级别">
                <i class="fa fa-chevron-left"></i> 动态调整日志级别
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/06/30/design-pattern/" rel="prev" title="设计模式">
                设计模式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.png" alt="Tonny Yi">
            
              <p class="site-author-name" itemprop="name">Tonny Yi</p>
              <p class="site-description motion-element" itemprop="description">Coding, Life, Zen</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">49</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">42</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">59</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/tonnyyi" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:tonnyemails@gmail.com" target="_blank" title="E-Mail"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  
                </span>
              
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc/4.0/" class="cc-opacity" target="_blank">
                <img src="/images/cc-by-nc.svg" alt="Creative Commons">
              </a>
            </div>
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#请求代理模式"><span class="nav-number">1.</span> <span class="nav-text">请求代理模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#规则总结"><span class="nav-number">1.1.</span> <span class="nav-text">规则总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#本地资源模式"><span class="nav-number">2.</span> <span class="nav-text">本地资源模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#nginx路由匹配规则"><span class="nav-number">3.</span> <span class="nav-text">nginx路由匹配规则</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#负载均衡配置"><span class="nav-number">4.</span> <span class="nav-text">负载均衡配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#典型配置"><span class="nav-number">4.1.</span> <span class="nav-text">典型配置</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tonny Yi</span>

  

  
</div>


  <script src="https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js"></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize("");
    }
  </script>





  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://theme-next.org">NexT.Mist</a></div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.3.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.3.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.3.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.3.0"></script>



  



	





  





  










  





  

  

  

  

  
  

  

  

  

  

  
  <style>
    .copy-btn {
      display: inline-block;
      padding: 6px 12px;
      font-size: 13px;
      font-weight: 700;
      line-height: 20px;
      color: #333;
      white-space: nowrap;
      vertical-align: middle;
      cursor: pointer;
      background-color: #eee;
      background-image: linear-gradient(#fcfcfc, #eee);
      border: 1px solid #d5d5d5;
      border-radius: 3px;
      user-select: none;
      outline: 0;
    }

    .highlight-wrap .copy-btn {
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 8px;
    }

    .highlight-wrap:hover .copy-btn,
    .highlight-wrap .copy-btn:focus {
      opacity: 1
    }

    .highlight-wrap {
      position: relative;
    }
  </style>
  <script>
    $('.highlight').each(function (i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap')
      $(e).after($wrap)
      $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function (e) {
        var code = $(this).parent().find('.code').find('.line').map(function (i, e) {
          return $(e).text()
        }).toArray().join('\n')
        var ta = document.createElement('textarea')
        document.body.appendChild(ta)
        ta.style.position = 'absolute'
        ta.style.top = '0px'
        ta.style.left = '0px'
        ta.value = code
        ta.select()
        ta.focus()
        var result = document.execCommand('copy')
        document.body.removeChild(ta)
        
          if(result)$(this).text('复制成功')
          else $(this).text('复制失败')
        
        $(this).blur()
      })).on('mouseleave', function (e) {
        var $b = $(this).find('.copy-btn')
        setTimeout(function () {
          $b.text('复制')
        }, 300)
      }).append(e)
    })
  </script>


</body>
</html>
